﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using kim.Model;
namespace kim.BLL
{
    /// <summary>
    /// Show
    /// </summary>
    public partial class Show
    {
        private readonly kim.DAL.Show dal = new kim.DAL.Show();
        public Show()
        { }
        #region  Method
        /// <summary>
        /// 分页控制
        /// </summary>
        /// <param name="tbname">要分页显示的表名</param>
        /// <param name="FieldKey">用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段</param>
        /// <param name="PageCurrent">要显示的页码</param>
        /// <param name="PageSize">每页的大小(记录数)</param>
        /// <param name="FieldShow">以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段</param>
        /// <param name="FieldOrder">以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC,用于指定排序顺序</param>
        /// <param name="Where">查询条件</param>
        /// <returns></returns>
        public DataSet GetNewsPager(int PageCurrent, int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            return dal.GetNewsPager("show", "id", PageCurrent, PageSize, FieldShow, FieldOrder, Where);
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(kim.Model.Show model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(kim.Model.Show model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            bool del = false;
            Model.Show smo = new BLL.Show().GetModel(ID);
            if (smo.TitlePic != "" || smo.TitlePic != null)
                del = kim.Common.Utils.DeleteFile(smo.TitlePic);
            if (smo.Pic != "" || smo.Pic != null)
                del = kim.Common.Utils.DeleteFile(smo.Pic);
            del = dal.Delete(ID);
            return del;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public kim.Model.Show GetModel(int ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public kim.Model.Show GetModelByCache(int ID)
        {

            string CacheKey = "ShowModel-" + ID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (kim.Model.Show)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<kim.Model.Show> GetModelList(int Top, string strWhere, string filedOrder)
        {
            DataSet ds = dal.GetList(Top, strWhere, filedOrder);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<kim.Model.Show> DataTableToList(DataTable dt)
        {
            List<kim.Model.Show> modelList = new List<kim.Model.Show>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                kim.Model.Show model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new kim.Model.Show();
                    if (dt.Rows[n]["ID"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    
                    if (dt.Rows[n]["TypeID"] != null && dt.Rows[n]["TypeID"].ToString() != "")
                    {
                        model.TypeID = int.Parse(dt.Rows[n]["TypeID"].ToString());
                    }

                    if (dt.Rows[n]["Sort"] != null && dt.Rows[n]["Sort"].ToString() != "")
                    {
                        model.Sort = int.Parse(dt.Rows[n]["Sort"].ToString());
                    }

                    if (dt.Rows[n]["ChannelId"] != null && dt.Rows[n]["ChannelId"].ToString() != "")
                    {
                        model.ChannelId = int.Parse(dt.Rows[n]["ChannelId"].ToString());
                    }

                    if (dt.Rows[n]["Title"] != null && dt.Rows[n]["Title"].ToString() != "")
                    {
                        model.Title = dt.Rows[n]["Title"].ToString();
                    }
                    if (dt.Rows[n]["TitlePic"] != null && dt.Rows[n]["TitlePic"].ToString() != "")
                    {
                        model.TitlePic = dt.Rows[n]["TitlePic"].ToString();
                    }
                    if (dt.Rows[n]["Remark"] != null && dt.Rows[n]["Remark"].ToString() != "")
                    {
                        model.Remark = dt.Rows[n]["Remark"].ToString();
                    }
                    if (dt.Rows[n]["Pic"] != null && dt.Rows[n]["Pic"].ToString() != "")
                    {
                        model.Pic = dt.Rows[n]["Pic"].ToString();
                    }
                    if (dt.Rows[n]["State"] != null && dt.Rows[n]["State"].ToString() != "")
                    {
                        if ((dt.Rows[n]["State"].ToString() == "1") || (dt.Rows[n]["State"].ToString().ToLower() == "true"))
                        {
                            model.State = true;
                        }
                        else
                        {
                            model.State = false;
                        }
                    }
                    if (dt.Rows[n]["Wurl"] != null && dt.Rows[n]["Wurl"].ToString() != "")
                    {
                        model.Wurl = dt.Rows[n]["Wurl"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

